// pages/mine/apply_record/apply_record.js
import {
  Toast
} from 'wuss-weapp';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      theme: app.globalData.theme
    });
    app.setNavigationBarColor();
    this.applyRecordLoad();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.globalData.pageTotalNumber = 0;
    app.globalData.pageNumber = 1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    app.globalData.pageNumber = 1;
    this.setData({
      result: []
    });
    this.applyRecordLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    app.globalData.pageNumber++;
    this.setData({
      mpLoadingShow: true,
      mpLoadingTips: "加载中..."
    });
    if (app.globalData.pageNumber <= app.globalData.pageTotalNumber) {
      this.applyRecordLoad();
    } else {
      setTimeout(() => {
        this.setData({
          mpLoadingTips: "暂无数据"
        });
      }, 500);
      setTimeout(() => {
        this.setData({
          mpLoadingShow: false
        });
      }, 1000);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 加载数据
   */
  applyRecordLoad: function () {
    app.wxGetRequest("/mine/getDeviceKeyList", {
      yardId: wx.getStorageSync("yard_id"),
      people_id: app.globalData.userInfo["people_id"]
    }).then(result => {
      let data = result.data.data;
      this.setData({
        result: data,
        noData: data.length == 0 ? true : false
      });
    });
  },
  /**
   * 开门
   */
  onStart: function (e) {
    let identification = e.currentTarget.dataset.identification;
    let title = e.currentTarget.dataset.title;
    app.confirm("温馨提示","确认开启『' + title + '』").then(()=>{
      app.wxGetRequest("/mine/unlockDevice", {
        identification: identification,
        user_id: app.globalData.userInfo[""]
      }).then(result => {
        let data = result.data.data;
        this.setData({
          result: data,
          noData: data.length == 0 ? true : false
        });
      });

      var url = 'mine/unlockDevice';
      var success = function (res) {
        if (res.data.code == 1) {
          wx.showModal({
            title: "温馨提示",
            content: res.data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {

              }
            }
          });
        } else {
          wx.showModal({
            title: "警告",
            content: res.data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {

              }
            }
          });
        }
      };
      var dataOptions = {
        identification: identification,
        user_id: app.globalData.userId
      };
      config.wxPostRequest(url, dataOptions, success);
    });
  },
})